<?php
class outGoodsEdit extends Admin {

    private $tab = array( 'outStock' => '出库管理','outGoodsEdit'=>'货品管理');
    private $color = array('深红', '粉红','橘红','橘黄 ',
        '玫瑰红','橙色','深黄','淡黄','草绿',
        '墨绿', '柠檬黄', '黄绿', '蓝绿', '浅绿',
        '天蓝', '湖蓝', '黑色', '白色', '紫色',
        '红灰', '黄灰', '蓝灰', '紫灰', '浅灰');
    private $size = array('080CM','085CM','090CM','095CM','100CM',"105CM","110CM",
        "115CM","120CM","125CM","130CM","135CM","140CM","145CM","150CM","155CM","160CM");

    public function __construct() {
        parent::__construct();
        $this->pk = "inOutStockNo";
    }

    public function index( $c, $a ) {

        $type = G( 'type', 'outGoodsEdit' );
        if('outGoodsEdit' != $type)
        {
            $method = "{$a}_{$type}";
            $this->$method( $c, $a, $type );
            return;
        }

        include $this->display( $c);
    }

    private function index_outStock( $c, $a, $type ) {
        header( "location:?c=outStock");
    }




    public function edit( $c, $a ) {
        $type = G( 'type', 'outStock' );
        if ( !$id = intval( G( 'inOutStockNo' ) ) ) {
            $this->error();
            exit;
        }

        $this->table = 'inOutStock'; // 数据库表名
        $result = $this->find( array( 'inOutStockNo' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }
        /**STEP1.查询出记录id为$id的出入库记录*/
        $this->table = 'inOutStockRec';
        $res = $this->find(array('id'=>$id));

        if(!$res)
        {
            return;
        }

        /***STEP2: 更新库存信息表**/
        $this->table = 'stockInfo';
        //$this->pk = "id";
        $where = ' goodsNo = ' . $res['goodsNo'] . ' and color = ' . $res['color']. ' and size = ' . $res['size'];
        if ($result = $this->find($where, 'id')) // 查找数据
        {
            $rows['goodsNumber'] = $result['goodsNumber'] -  $res['goodsNumber'];
            $this->update(array('id'=>$id),$rows);
        }

        /***STEP3: 更新出入库信息表**/
        unset($rows);
        $this->table = 'inOutStock';
        $inOutStockNo = $res['inOutStockNo'];
        $result = $this->find(array('inOutStockNo'=>$inOutStockNo));
        if($result)
        {
            $rows['inOutRecList'] = str_replace(G('id'),"",$result['inOutRecList']);
            $this->update(array('inOutStockNo'=>$inOutStockNo),$rows);
        }

        /***STEP4: 删除出入库记录表**/
        $this->table = 'inOutStockRec';
        $this->delete(array('id'=>$id));


    }

    public function search_outStock( $c, $a ) {
        $this->table = 'inOutStock'; // 数据库表名
        $type = G( 'type', 'outStock' );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "inOutStockNo ={$key} " ) : '';
        }

        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $res = $result[ 'result' ];


        include $this->display( $c ); // 加载模板


    }


    public function search_Goods( $c, $a ) {


        $this->table = 'inOutStockRec'; // 数据库表名


        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "inOutStockNo ={$key} " ) : '';
        }

        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $res = $result[ 'result' ];

        foreach($res as $key =>$val)
        {
            $res[$key]['color'] = $this->color[$val['color']];
            $res[$key]['size'] = $this->size[$val['size']];
        }
        $result['key'] = G( 'key' );

        include $this->display( $c ); // 加载模板


    }

    public function addGoods($c,$a,$args)
    {
        $type = G('type');
        $result =array('inOutStockNo'=>G('inOutStockNo'),'editMode'=>0);

        print_r($result['editMode']) ;

        include $this->display($c);
    }

    public function editGoods($c,$a,$args)
    {
        $id = G('id');
        $this->table = 'inOutStockRec'; // 数据库表名

        $result = $this->find(array('id'=>$id));
        $result['inOutStockNo'] = G('inOutStockNo');
        $result['editMode'] = 1;

        $result['size'] = $this->size[$result['size']];
        $result['color'] = $this->color[$result['color']];

        include $this->display($c);
    }



    public function updateGoods()
    {
        /*商品
         * 1.更新商品基本信息表
         * 2.更新出入库记录表
         * 3. 更新库存信息表    stockInfo
         * 4. 价格表更新        priceDescribe
         *
         * */

        /*STEP1.更新商品基本信息表
        */
        $this->table = 'bsInfo';

        $rows['materialNo'] = $_POST['clothNo'];
        $this->update(array('goodsNo'=>$_POST['goodsNo']),$rows);




        $rows['inOutStockNo'] = $_POST['inOutStockNo'];
        /*STEP2.更新出入库记录表
              */
        $this->table = 'inOutStockRec';
        $id = G('id');
        $rows['goodsNumber'] = $_POST['goodsNumber'];

        $result = $this->find(array('id'=>$id));
        $number = $result['goodsNumber'];

        if ($result)// 查找数据
        {

            $this->update(array('id'=>$id),$rows);

        }
        /*STEP3.更新出入库记录表
                     */


        unset($rows);
        $this->table = 'stockInfo';
        //$this->pk = "id";
        $where = ' goodsNo = ' . $_POST['goodsNo'] . ' and color = ' . array_search($_POST['color'],$this->color). ' and size = ' . array_search($_POST['size'],$this->size);
        if ($result = $this->find($where, 'id')) // 查找数据
        {
            $rows['goodsNumber'] = $result['goodsNumber'] - $number +  $_POST['goodsNumber'];
            $id = $result['id'];
            $this->update(array('id'=>$id),$rows);
        }

        unset($rows);
        $this->table = 'priceDescribe';
        $rows['costPrice'] = $_POST['costPrice'];
        $this->update(array('goodsNo'=>$_POST['goodsNo']),$rows);

    }

    public function addSaveGoods()
    {
        /*   0： 入库  ； 1：出库
         *   1. 商品信息表加入记录
         *   2. 更新出入库表单，更新出入库记录id列表   inOutStock
         *   3. 出入库记录表添加记录  inoutStockRec
         *   4. 更新库存信息表    stockInfo
         *   5. 价格表更新        priceDescribe
         */
        /**STEP1: 商品信息表bsInfo加入记录**/

        $this->table = 'bsInfo';
        $sql="select max(id) from xbl_bsInfo";
        $res = $this->findSql($sql);



        $rows['id'] = $res[0]['max(id)'] + 1;
        $rows['goodsNo'] = $_POST['goodsNo'];
        $rows['materialNo']   = $_POST['clothNo'];
        $rows['supplierName'] = $_POST['supplierName'];
        foreach($_POST['color'] as $key=>$val)
        {
            $rows['color'] = $_POST['color'][$key].','.$rows['color'];
        }
        foreach($_POST['size'] as $key=>$val)
        {
            $rows['size'] = $rows['size'].$_POST['size'][$key];
        }
        $rows['goodsStatus'] = 0;
        $msg = $this->create( $rows ) ? 'success' : 'failed';


        /**STEP2: 更新出入库表inOutStock，添加可选尺码，可选颜色值*
         *   （1）确定出入库记录Id
         *   （2）确定更新内容项
         *   （3）更新出入库表
         */


        /**
         * （1）确定出入库记录Id
         */
        $this->table = "inOutStockRec";
        $res = $this->find(null, null, "max(id)");
        $maxid = intval($res['max(id)']) + 1;
        print_r($res);


        /**
         * （2）确定更新内容项
         */
        $this->table = 'inOutStock'; // 数据库表名
        $condition = array('inOutStockNo' => $_POST['inOutStockNo']);
        $this->pk = 'inOutStockNo';
        $result = $this->find($condition);
        /**zouhong-edit**/
        $rows[ 'inOutStockNo' ] = $_POST['inOutStockNo'];
        $rows['inOutRecList']= $result['inOutRecList'].$maxid.",";
        print_r( $rows['inOutRecList']);

        /**
         * （3）更新出入库表
         */
        /**zouhong-edit-edit**/
        $this->table = 'inOutStock'; // 数据库表名
        $msg = $this->update($condition,$rows) ? 'success' : 'failed';

        unset($rows);
        unset($result);
        unset($res);


        /**STEP3: 更新出入库表inOutStockRec，
         *
         */

        $rows['goodsNo'] = htmlspecialchars( trim( $_POST['goodsNo'] ) );
        $rows[ 'clothNo' ] = htmlspecialchars( trim( $_POST['clothNo'] ) );
        //**更新出入库记录表、商品库存信息表*
        $this->table = 'inOutStockRec'; // 数据库表名
        $rows['inOutStockNo'] = $_POST['inOutStockNo'];
        $rows['inOutStyle']= 0 ;

        if(count($_POST['color']) > 1)
        {
            $judgeVal = 1;           /***判断同一款衣服有不同颜色**/
        }
        else
        {
            $judgeVal = 0;         /***判断同一款衣服有不同尺码**/
        }

        $rows['goodsNumber'] = $_POST['goodsNumber'];
        if(1 == $judgeVal)
        {
            foreach ( $_POST['color'] as $key => $val ) {

                /**STEP3:出入库记录表添加记录  inoutStockRec
                 *
                 */
                $this->table = 'inOutStockRec';
                $rows['color'] = $val;
                $rows['size'] =  $_POST['size'][0];

                $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商


                /**STEP4:跟新库存信息表    stockInfo
                 *
                 */
                $this->table = 'stockInfo';
                //$this->pk = "id";
                $where =  ' goodsNo = '.$rows['goodsNo'].' and color = '.$rows['color'].' and size = '.$rows['size'];
                if ($result = $this->find($where, 'id' )) // 查找数据
                {

                    $sql = "update xbl_stockInfo set goodsNumber = goodsNumber + ".$rows['goodsNumber'].' where '.$where;
                    $this->exec($sql);
                }
                else
                {
                    $id = $this->create( $rows );
                    $msg = $id ? 'success' : 'failed';
                }
                //$this->pk = "inOutStockNo";
            }
        }
        else
        {
            foreach ( $_POST['size'] as $key => $val ) {

                /*
                 * STEP3:出入库记录表添加记录  inoutStockRec
                 *
                 */
                $this->table = 'inOutStockRec';
                $rows['size'] = $val;
                $rows['color'] = $_POST['color'][0];

                $msg = $this->create( $rows ) ? 'success' : 'failed';

                /**STEP4:跟新库存信息表    stockInfo
                 *
                 */

                $this->table = 'stockInfo';
                //$this->pk = "id";
                $where =  ' goodsNo = '.$rows['goodsNo'].' and color = '.$rows['color'].' and size = '.$rows['size'];
                if ($result = $this->find($where, 'id' )) // 查找数据
                {

                    $sql = "update xbl_stockInfo set goodsNumber = goodsNumber + ".$rows['goodsNumber'].' where '.$where;  //补货添加库存
                    $this->exec($sql);
                }
                else
                {
                    $id = $this->create( $rows );
                    $msg = $id ? 'success' : 'failed';
                }
                //$this->pk = "inOutStockNo";
            }
        }

        /**STEP5: 商品价格表添加记录
         *
         */
        unset($rows);
        $this->table = 'priceDescribe';
        $rows['goodsNo'] = $_POST['goodsNo'];
        $rows['costPrice'] = $_POST['costPrice'];

        try{
            $id = $this->create( $rows );
            // $msg = $id ? 'success' : 'failed';
        }catch (Exception $e)
        {
            //   print $e->getMessage();
        }


        exit( $msg );
    }

    public function saveGoods($c,$a,$args) {



        if(0 == $_POST['editMode'])
        {
            $this->addSaveGoods();
        }
        else
        {
            $this->updateGoods();

        }


    }


}
